package cn.client.ui.view.main.mindfulness

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import cn.client.data.remoteData.bean.MindfulnessMedia
import cn.client.data.remoteData.bean.PageSelectByUid
import cn.client.data.userStateManager
import cn.client.navigation.destinations.MindfulnessExercisePage
import coil.compose.AsyncImage

// 正念练习资源
@Composable
fun MindfulnessMedia(
    navController: NavController,
    viewModel: MindfulnessViewModel
){

    val userIdState = userStateManager.userId.collectAsState(initial = -1)
    val userId = userIdState.value

    // 观察viewModel中的状态
    val recommendMindfulnessState =
        viewModel.recommendMindfulness.collectAsState(initial = emptyList())
    val recommendMindfulness = recommendMindfulnessState.value

    val pageSelectByUid = PageSelectByUid(uid = userId, pageNo = 1, pageSize = 10)

    if (userId != -1) {
        viewModel.dispatch(MindfulnessViewModel.PageIntent.GetRecommendMindfulness(pageSelectByUid))
    }

    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        LazyColumn(
            modifier = Modifier
        ){
            items(recommendMindfulness){
                MindfulnessMediaItem(
                    media = it,
                    onClickMindfulnessMediaItem = {
                        navController.navigate(MindfulnessExercisePage.route + "/${it.id}")
                    }
                )
            }
        }
    }
}

@Composable
fun MindfulnessMediaItem(
    media: MindfulnessMedia,
    onClickMindfulnessMediaItem:() -> Unit
){
    var isExpanded by remember { mutableStateOf(false) }

    Row(
        modifier = Modifier
            .padding(5.dp)
            .fillMaxWidth()
    ) {
        AsyncImage(
            model = media.imgUrl,
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(88.dp)
                .clip(CircleShape)
                .clickable {
                onClickMindfulnessMediaItem()
                }
        )
        Column(
            modifier = Modifier
                .padding(start = 5.dp)
                .fillMaxWidth()
        ) {
            Text(
                text = media.mediaName,
                modifier = Modifier,
                style = MaterialTheme.typography.bodyMedium,
                fontSize = 20.sp
            )
            Text(
                text = media.mediaInfo,
                modifier = Modifier
                    .fillMaxWidth()
                    .clickable {
                        isExpanded = !isExpanded
                    },
                style = MaterialTheme.typography.bodyMedium,
                maxLines = if (isExpanded) Int.MAX_VALUE else 3,
                color = Color.Gray,
                fontSize = 12.sp
            )
        }

    }
}